<?php

namespace app\api\controller\user;

use think\Controller;


class Base extends Controller{

    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
        $this->ContactModel = model('Contact.Contact');
    }

    // 登陆
    public function login(){
        if($this->request->isPost())
        {
            $email = $this->request->param('email', '');
            $password = $this->request->param('password', '');
            
            //查询 toArray 转换为数组
            $User = $this->UserModel->where(['email' => $email])->find();

            //当没有找到用户的情况下
            if(!$User)
            {
                $this->error('用户不存在，请重新输入');
                exit;
            }

            //密码盐
            $salt =  $User['salt'];

            //要让密码加密后做对比
            $password = md5(trim($password).$salt);

            //判断密码
            if($password != $User['password']){

                $this->error('密码输入有误，请重新输入');
                exit;
            }

             //拼接返回的数据
             $data = [
                'id'=>$User['id'],
                'email'=>$User['email'],
                'nickname'=>$User['nickname'],
                'avatar'=>$User['avatar_cdn'],
                'sex'=>$User['sex'],
                'province'=>$User['province'],
                'city'=>$User['city'],
                'district'=>$User['district'],
                'createtime'=>$User['createtime']
            ];

            //跳转界面
            $this->success('登录成功', '/user/base/index', $data);
            exit;
        }
    }

    // 注册
    public function register(){
        if($this->request->isPost()){

            $email = $this->request->param('email', '');
            $password = $this->request->param('password', '');

            //查询邮箱
            $User = $this->UserModel->where(['email' => $email])->find();

            if($User){

                $this->error('该邮箱已注册，请重新输入');
                exit;
            }

            if(empty($password))
            {
                $this->error('密码不能为空');
                exit;
            }

            //生成密码盐
            $salt = randstr(10);

            //将密码和密码盐md5加密
            $password = md5(trim($password).$salt);

            //组装数据
            $data = [
                'email'=>$email,
                'password'=>$password,
                'salt'=>$salt,
                'nickname'=>$email
            ];

            //开始插入数据库 
            $result = $this->UserModel->validate('common/User/User')->save($data);

            if($result === FALSE)
            {
                //注册失败
                // code = 0
                // getError 返回操作的结果
                $this->error($this->UserModel->getError());
                exit;
            }else
            {
                //注册成功，跳转到登录界面
                // code = 1
                $this->success('注册成功');
                exit;
            }
        }
    }
    
    // 验证是否登录
    public function check() {
        if($this->request->isPost()){

            $userid = $this->request->param('userid', 0);

            $User = $this->UserModel->find($userid);

            if($User){
                //设置一个保存cookie信息
                $data = [
                    'id'=>$User['id'],
                    'email'=>$User['email'],
                    'nickname'=>$User['nickname'],
                    'avatar'=>$User['avatar_cdn'],
                    'sex'=>$User['sex'],
                    'province'=>$User['province'],
                    'city'=>$User['city'],
                    'district'=>$User['district'],
                    'createtime'=>$User['createtime'],
                    'cover'=>$User['cover_cdn'],
                    'region'=>$User['region'],
                ];

                $this->success('登录成功，存在用户', null, $data);
                exit;
                
            } else{
                $this->error('用户不存在');
                exit;
            }
        }
    }

    // 修改资料
    public function profile()
    {
        if($this->request->isPost()){
           
            //获取所有的参数
            $params = $this->request->param();
           
            $userid = $this->request->param('id', 0);
            $password = $this->request->param('password', '');

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

          

            //组装数据
            $data = [
                'id'=>$userid,
                'nickname'=>$params['nickname'],
                'email'=>$params['email'],
                'sex'=>$params['sex'],
            ];

            //密码不为空
            if(!empty($password))
            {
                $salt = $user['salt'];

                $repass = md5($password.$salt);

                //判断新密码和旧密码是否一致
                if($repass == $user['password'])
                {
                    $this->error('当前密码为旧密码，请重新输入');
                    exit;
                }

                //生成新的密码盐
                $newsalt = randstr(10);

                //新密码加密
                $password = md5(trim($password).$newsalt);

                $data['salt'] = $newsalt;
                $data['password'] = $password;
            }

            //判断头像
            if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0)
            {
                $result = upload('avatar');

                if($result === FALSE)
                {
                    $this->error('修改头像失败');
                    exit;
                }

                //追加头像
                $data['avatar'] = 'uploads/'.$result;
            }
           

            //更新数据库
            $result = $this->UserModel->isUpdate()->save($data);

            // echo $this->UserModel->getLastSql();
            // exit;

            if($result === FALSE)
            {
                //上传上来的图片删除了
                @is_file($data['avatar']) && @unlink($data['avatar']);

                @is_file($data['cover']) && @unlink($data['cover']);

                $this->error('修改个人资料失败');
                exit;
            }else
            {
                //先判断是否有选择新的图片
                if(@is_file($data['avatar']))
                {
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($user['avatar'],"/");
                    @is_file($avatar) && @unlink($avatar);
                }

                //直接查询最新的结果在返回
                $UserData = $this->UserModel->find($userid);

                $data = [
                    'id'=>$UserData['id'],
                    'email'=>$UserData['email'],
                    'nickname'=>$UserData['nickname'],
                    'avatar'=>$UserData['avatar_cdn'],
                    'sex'=>$UserData['sex'],
                    'province'=>$UserData['province'],
                    'city'=>$UserData['city'],
                    'district'=>$UserData['district'],
                    'createtime'=>$UserData['createtime']
                ];

                $this->success('修改资料成功', '/user/base/index', $data);
                exit;
            }
        }
    }

    // 删除信息
    public function del()
    {
        if($this->request->isPost())
        {
            $id = $this->request->param('id', 0);
            $userid = $this->request->param('userid', 0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $list = $this->ContactModel->find($id);

            if(!$list){

                $this->error('没有该通讯记录');
                exit;
            }

            $result = $this->ContactModel->destroy($id);

            if($result === FALSE)
            {
        
                $this->error($this->ContactModel->getError());
                exit;
            }else
            {
                // code = 1
                $this->success('删除成功');
                exit;
            }


            var_dump($id);
            exit;
        }
    }

}